package com.icim.util;

import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.icim.exception.ICIMException;

public class FileUtil {
    public final static String XML = ".XML";
    public final static String GZ = ".GZ";
    public final static Logger logger = LoggerFactory.getLogger(FileUtil.class);

    /**
     * 
     * @param localPath
     * @param fileExt
     * @return
     */
    public static File[] listFiles(final String localPath, final String fileExt) {

        File director = new File(localPath);

        if (!director.isDirectory()) {
            throw new ICIMException(localPath + " 不是有效路径");
        }

        File[] files = director.listFiles(new FileFilter() {

            @Override
            public boolean accept(File file) {
                if (file.getName().toUpperCase().endsWith(fileExt)) {
                    return true;
                }
                return false;
            }
        });

        return files;
    }
    
    /**
     * 
     * @param localPath
     * @param fileExt
     * @return
     */
    public static File[] listFiles(final String localPath) {

        File director = new File(localPath);

        if (!director.isDirectory()) {
            throw new ICIMException(localPath + " 不是有效路径");
        }

        File[] files = director.listFiles();

        return files;
    }


    public static void deleteFile(File file) {
        file.getAbsoluteFile().delete();
    }

    public static void deleteFileOnExit(File file) {
        file.getAbsoluteFile().deleteOnExit();
    }

    /**
     * 
     * @param localPath
     * @param fileExt
     */
    public static void deleteAllFiles(String localPath, String fileExt) {
        File[] files = listFiles(localPath, fileExt);

        for (File file : files) {
            file.getAbsoluteFile().delete();
        }
    }
    
    /**
     * 
     * @param localPath
     * @param fileExt
     */
    public static void deleteAllFiles(String localPath) {
        File[] files = listFiles(localPath);

        for (File file : files) {
            file.getAbsoluteFile().delete();
        }
    }

    public static void sting2File(String fileName, String content) throws Exception {
        RandomAccessFile mm = null;
        FileOutputStream o = null;
        try {
            o = new FileOutputStream(fileName);
            o.write(content.getBytes("UTF-8"));
            o.close();
        } catch (Exception e) {
            throw e;
        } finally {
            if (mm != null) {
                try {
                    mm.close();
                } catch (IOException e) {
                    logger.info(e.toString());
                }
            }
        }
    }

    public static void removeFile(String fileName, String fromPath, String toPath) {
            File file = new File(fromPath + "/" + fileName);
            if (file.renameTo(new File(toPath + "/" + file.getName()))) {
                logger.info("move file [{}] from {} to {} successful!",fileName,fromPath,toPath);
            } else {
                logger.info("move file [{}] from {} to {} failed",fileName,fromPath,toPath);
            }
    }
}
